package com.huizu.project.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huizu.common.core.domain.AjaxResult;
import com.huizu.common.utils.DateUtils;
import com.huizu.common.utils.SnowFlake;
import com.huizu.project.domain.ProSpPraise;
import com.huizu.project.domain.ProSpReview;
import com.huizu.project.mapper.ProSpPraiseMapper;
import com.huizu.project.mapper.ProSpReviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huizu.project.mapper.ProSpMapper;
import com.huizu.project.domain.ProSp;
import com.huizu.project.service.IProSpService;

/**
 * 视频管理Service业务层处理
 * 
 * @author zhangmx
 * @date 2024-06-05
 */
@Service
public class ProSpServiceImpl implements IProSpService 
{
    @Autowired
    private ProSpMapper proSpMapper;

    @Autowired
    private ProSpReviewMapper proSPreviewMapper;

    @Autowired
    private ProSpPraiseMapper proSpPraiseMapper;

    /**
     * 查询视频管理
     * 
     * @param id 视频管理主键
     * @return 视频管理
     */
    @Override
    public ProSp selectProSpById(String id)
    {
        return proSpMapper.selectProSpById(id);
    }

    /**
     * 查询视频管理列表
     * 
     * @param proSp 视频管理
     * @return 视频管理
     */
    @Override
    public List<ProSp> selectProSpList(ProSp proSp)
    {
        return proSpMapper.selectProSpList(proSp);
    }

    /**
     * 新增视频管理
     * 
     * @param proSp 视频管理
     * @return 结果
     */
    @Override
    public int insertProSp(ProSp proSp)
    {
        proSp.setId(String.valueOf(SnowFlake.nextId()));
        proSp.setCreateTime(DateUtils.getNowDate());
        return proSpMapper.insertProSp(proSp);
    }

    /**
     * 修改视频管理
     * 
     * @param proSp 视频管理
     * @return 结果
     */
    @Override
    public int updateProSp(ProSp proSp)
    {
        return proSpMapper.updateProSp(proSp);
    }

    /**
     * 批量删除视频管理
     * 
     * @param ids 需要删除的视频管理主键
     * @return 结果
     */
    @Override
    public int deleteProSpByIds(String[] ids)
    {
        return proSpMapper.deleteProSpByIds(ids);
    }

    /**
     * 删除视频管理信息
     * 
     * @param id 视频管理主键
     * @return 结果
     */
    @Override
    public int deleteProSpById(String id)
    {
        return proSpMapper.deleteProSpById(id);
    }

    /**
     * 新增视频管理
     *
     * @param proSp 视频管理
     * @return 结果
     */
    public int insertProSpApi(ProSp proSp){
        proSp.setId(String.valueOf(SnowFlake.nextId()));
        proSp.setCreateTime(new Date());

        return proSpMapper.insertProSp(proSp);
    }

    public AjaxResult review(ProSpReview proSpReview){
        proSpReview.setId(String.valueOf(SnowFlake.nextId()));
        proSpReview.setCreateTime(new Date());
        proSPreviewMapper.insertProSpReview(proSpReview);
        return AjaxResult.success("评论成功");
    }

    public List<ProSpReview> listReview(ProSpReview proSpReview){
        return proSPreviewMapper.selectProSpReviewList(proSpReview);
    }

    public AjaxResult updatePraise(ProSpPraise proSpPraisePram){
        ProSpPraise proSpPraise = proSpPraiseMapper.selectProSpPraiseByUserAndId(proSpPraisePram);
        if (proSpPraise==null){
            proSpPraisePram.setId(String.valueOf(SnowFlake.nextId()));
            proSpPraisePram.setCreateTime(new Date());
            proSpPraiseMapper.insertProSpPraise(proSpPraisePram);
            return AjaxResult.success("点赞成功");
        }else {
            int i = proSpPraiseMapper.deleteProSpPraiseById(proSpPraise.getId());
            return AjaxResult.success("取消成功");
        }
    }

    /**
     * 获取点赞列表
     * @param proSpPraise
     * @return
     */
    public List<ProSp> listPraise(ProSpPraise proSpPraise){
        List<ProSp> respList = new ArrayList<>();
        List<ProSpPraise> proSpPraises = proSpPraiseMapper.selectProSpPraiseList(proSpPraise);
        for (ProSpPraise spPraise : proSpPraises) {
            ProSp proSp = proSpMapper.selectProSpById(spPraise.getpId());
            if (proSp!=null){
                respList.add(proSp);
            }
        }
        return respList;
    }
}
